<?php
require_once("includes/pagetop.php");
if($_FILES["selectedFile"]!="")
{
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		if(isset($_REQUEST["page"]) && $_REQUEST["page"]!="")
	    {
	    	$pageID=$_REQUEST["page"];
			$qryPageDetail="select * from af_company_page where Page_Slug=:Page_Slug and User_ID=:User_ID and Status=0";
			$arrPageDetail=$pdodbobj->fetch_array($pdodbobj->query($qryPageDetail,array(":Page_Slug"=>$pageID,":User_ID"=>$_SESSION["Allyfolio_User_ID"])));
			if(is_array($arrPageDetail) && count($arrPageDetail)>0)
			{
				$ext = explode('.', basename( $_FILES['selectedFile']['name']));
				if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif","vob","mp4","3gp")))
				{
					$target_path = 'uploads/page_media/'.$arrPageDetail[0]["Comp_Page_ID"];
					if(!file_exists($target_path))
			        {
						mkdir($target_path,0777);
					}
					$fileName=$_FILES['selectedFile']['name'];
					$Fullpath=$target_path ."/".$fileName;
					
					//if(move_uploaded_file($_FILES['selectedFile']['tmp_name'], $Fullpath))
//					{
//						echo "IF";
//						echo ":".exec("ffmpeg -i ".$Fullpath." ".$target_path ."/".$ext[0].".flv").":"; 
//					}
//					else
//					{
//						echo "ELSE";
//					}
//					die("vikas testing");
					
					if(move_uploaded_file($_FILES['selectedFile']['tmp_name'], $Fullpath)) 
					{
						if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif")))
						{
							$mediaType="IMAGE";
						}
						else if(in_array(strtolower($ext[count($ext)-1]),array("vob","mp4","3gp")))
						{
							$mediaType="VIDEO";
						}
						
						//Save in DB
						$qryInsert="insert into af_company_page_media set Comp_Page_ID=:Comp_Page_ID, Media_Path=:Media_Path, Media_Type=:Media_Type,updated_date='".time()."'";
						$pdodbobj->query($qryInsert,array(":Comp_Page_ID"=>$arrPageDetail[0]["Comp_Page_ID"],":Media_Path"=>$Fullpath,":Media_Type"=>$mediaType ));
						echo "SUCCESS|~|".$Fullpath."|~|".$mediaType;
					}
					else
					{
						echo "ERROR|~|Looks like there is some problem. Please try after some time..";
					}
				}
				else
				{
					echo "ERROR|~|Please Upload image and video only.";
				}
			}
			else
			{
				echo "ERROR|~|Looks like there is some problem. Please try after some time...";
			}
		}
	}
}
else if($_REQUEST["purpose"]!="" && $_REQUEST["purpose"]=="delete" && $_REQUEST["mediaID"]!="" && $_REQUEST["mediaID"]>0 && $_REQUEST["page"]!=""  )
{
	$pageID=$_REQUEST["page"];
	$qryPageDetail="select cpm.Comp_Page_ID from af_company_page_media as cpm, af_company_page as cp where cpm.Comp_Page_ID= cp.Comp_Page_ID and cp.User_ID =:User_ID and cpm.Media_Upload_ID=:Media_Upload_ID and cp.Page_Slug=:Page_Slug";
	$arrPageDetail=$pdodbobj->fetch_array($pdodbobj->query($qryPageDetail,array(":Media_Upload_ID"=>$_REQUEST["mediaID"],":User_ID"=>$_SESSION["Allyfolio_User_ID"],":Page_Slug"=>$pageID)));

	if(is_array($arrPageDetail) && count($arrPageDetail)>0)
	{
		$qryDelete="update af_company_page_media set Status=2, delete_date='".time()."' where Media_Upload_ID=:Media_Upload_ID";
		$pdodbobj->query($qryDelete,array(":Media_Upload_ID"=>$_REQUEST["mediaID"] ));
		echo "SUCCESS|~|Page media has been deleted successfully.";
	}
	else
	{
		echo "ERROR|~|Looks like there is some problem. Please try after some time.";
	}
}
else if($_FILES["Comp_Page_Pic_file"]!="" && $_REQUEST["Comp_Page_Pic_No_hidden"]>0 && $_REQUEST["Comp_Page_ID_hidden"]!="" )
{
	$fileControlID="Comp_Page_Pic_file";
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		$ext = explode('.', basename( $_FILES[$fileControlID]['name']));
		if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif")))
		{
			$display_path = 'uploads/comp_page_images/'.$_SESSION['Allyfolio_User_ID'];
			$target_path=$display_path;
			if(!file_exists($target_path))
	        {
				mkdir($target_path,0777);
			}
			$fileName=$_FILES[$fileControlID]['name'];
			$Fullpath=$target_path ."/".$fileName;
			if(move_uploaded_file($_FILES[$fileControlID]['tmp_name'], $Fullpath)) 
			{
				$qryUpdate="update af_company_page set Comp_Page_Pic".$_REQUEST["Comp_Page_Pic_No_hidden"]."=:Comp_Page_Pic where Comp_Page_ID='".$_REQUEST["Comp_Page_ID_hidden"]."'";
				$pdodbobj->query($qryUpdate,array(":Comp_Page_Pic"=>$display_path."/".$fileName ));
				
				echo "SUCCESS|~|".$display_path."/".$fileName;
			}
			else
			{
				echo "ERROR|~|Looks like there is some problem. Please try after some time.";
			}
		}
		else
		{
			echo "ERROR|~|Please Upload images only.";
		}
	}
}
?>